package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.fight.module.game.config.BuffConfig;
import com.sghd.fight.module.game.config.ElementConfig;
import com.sghd.fight.module.game.config.ElementPk;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.model.Buff;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.skill.ConfigService;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.*;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author frank
 * @date 2018-12-18 15:12
 * @desc 雷霆一击、
 **/
@Component
public class ThunderboltBlowEffect implements Effect {
    @Autowired
    private ConfigService configService;

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        //上一个击倒的buff
        int buffId = MapUtils.getInteger(ctx, Effect.BUFF_ID);
        BuffConfig buffConfig = configService.getBuff(buffId);
        int extrTime = 0;
        //额外BUFF时间
        if (owner instanceof Player) {
            Map<ChessType, Integer> attr2Level = ((Player) owner).getElement2Level();
            int elLevel = attr2Level.get(config.getType());
            for (; elLevel > 0; elLevel--) {
                ElementPk pk = ElementPk.valueOf(config.getType(), elLevel);
                ElementConfig attrConfig = room.getConfigService().getAttr(pk);
                if (logger.isDebugEnabled()) {
                    logger.debug("玩家[{}]五行额外BUFF时间：{}", owner.getId(), attrConfig.getAttr(room.getType()).getExtraCtrl());
                }
                //五行额外控制
                extrTime += attrConfig.getAttr(room.getType()).getExtraCtrl();
            }
        }
        ctx.put(EXTRTIME, extrTime);
        Buff buff = getBuff(config, buffConfig, ctx, owner, target);
        context.addBuff(target, buff);
        targetReport.addBuff(BuffReport.valueOf(buffId, buff.getType(), buff.getValue(), buff.getTime(), buff.getLevel()));
        //造成10点基础伤害
        int attack = MapUtils.getIntValue(ctx, ATTACK);

        //技能等级提供的伤害加成
        int level = owner.getSkillLevel(config.getId());
        Map<String, Object> attack2Level = MapUtils.getMap(ctx, String.valueOf(level));
        attack += MapUtils.getIntValue(attack2Level, ATTACK);

        //计算公式 扣血
        int val = (int) (attack * getBet(owner, target));
        val = fixHp(owner, target, val, room, config);
        context.addValue(target, UnitValue.HP, -val);
        targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -val));

    }

    @Override
    public EffectType getType() {
        return EffectType.SK_THUNDERBOLT_BLOW;
    }
}
